//
//  EmailSendViewController.m
//  哎游
//
//  Created by 林程杰 on 15/1/17.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "EmailSendViewController.h"
#import "SKPSMTPMessage.h"
#import "NSData+Base64Additions.h"
#import "WebColor.h"
#import "Masonry.h"


@implementation EmailSendViewController


- (void)dealloc
{
    [_field release];
    [_filed1 release];
    [_buttonSend release];
    [_alert release];
    [super dealloc];
}

- (void) viewDidLoad
{
    [super viewDidLoad];
    [self.view setBackgroundColor:[WebColor whiteSmoke]];
    
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 25, 25)];
    [button setBackgroundImage:[UIImage imageNamed:@"return.png"] forState:UIControlStateNormal];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    [button addTarget:self action:@selector(returnBack) forControlEvents:UIControlEventTouchUpInside];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 0, 45, 100)];
    label.textColor = [UIColor whiteColor];
    label.font = [UIFont fontWithName:@"TimesNewRomanPS-BoldMT" size:20];
    label.text = @"意见反馈";
    self.navigationItem.titleView = label;
    [button release];
    [label release];

    [self creatView];
}

- (void) creatView
{
    self.field = [[UITextField alloc] init];
    [self.view addSubview:_field];
    [_field mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(25);
        make.top.equalTo(self.view.mas_top).offset(self.view.frame.size.height / 5);
        make.size.mas_equalTo(CGSizeMake(self.view.frame.size.width - 50, 150));
    }];
   _field.placeholder = @"有意见你就提 !";
    _field.clearsOnBeginEditing = YES;
    _field.borderStyle = UITextBorderStyleRoundedRect;

    self.filed1 = [[UITextField alloc] init];
    [self.view addSubview:_filed1];
    [_filed1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(25);
        make.top.equalTo(_field.mas_bottom).offset(15);
        make.size.mas_equalTo(CGSizeMake(self.view.frame.size.width - 50, 40));
    }];
    _filed1.placeholder = @"输入你的邮箱, 便于反馈";
    _filed1.clearsOnBeginEditing = YES;
    _filed1.borderStyle = UITextBorderStyleRoundedRect;
    
    self.buttonSend = [[UIButton alloc] init];
    [self.view addSubview:_buttonSend];
    [_buttonSend mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(25);
        make.top.equalTo(_filed1.mas_bottom).offset(20);
        make.size.mas_equalTo(CGSizeMake(self.view.frame.size.width - 50, 40));
    }];
    [_buttonSend setTitle:@"点击发送反馈信息" forState:UIControlStateNormal];
    [_buttonSend setBackgroundColor:[WebColor seaGreen]];
    [_buttonSend setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_buttonSend setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
    [_buttonSend addTarget:self action:@selector(sendEmail :) forControlEvents:UIControlEventTouchUpInside];
}

- (void) returnBack
{
    [self.navigationController popViewControllerAnimated:YES];
}


- (void) sendEmail : (id) sender
{
    if ([_filed1.text isEqualToString:@""] || _filed1.text.length == 0) {
        self.alert = [[UIAlertView alloc] initWithTitle:@"请输入您的邮箱便于我们反馈" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
        [_alert show];
        [_alert release];
        [NSTimer scheduledTimerWithTimeInterval:1.5f target:self selector:@selector(timerAction :) userInfo:nil repeats:YES];
    }else{
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"谢谢您的宝贵意见" message:@"我们会尽快改进" delegate:self cancelButtonTitle:@"发送" otherButtonTitles:@"取消", nil];
        [alert show];
        [alert release];
    }
}

- (void) alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {
        SKPSMTPMessage *testMsg = [[SKPSMTPMessage alloc] init];
        testMsg.fromEmail = @"aiyouxiangmuzu@163.com";
        testMsg.toEmail = @"562189177@qq.com";
        testMsg.relayHost = @"smtp.163.com";
        testMsg.requiresAuth = YES;
        if (testMsg.requiresAuth) {
            testMsg.login = @"aiyouxiangmuzu@163.com";
            testMsg.pass = @"jj5211";
        }
        
        testMsg.wantsSecure = YES;
        testMsg.subject = @"反馈意见";
        testMsg.delegate = self;
        NSString *str = [_field.text stringByAppendingString:_filed1.text];
        NSDictionary *plainPart = [NSDictionary dictionaryWithObjectsAndKeys:@"text/plain; charset=UTF-8",kSKPSMTPPartContentTypeKey,
                                   str,kSKPSMTPPartMessageKey,@"8bit",kSKPSMTPPartContentTransferEncodingKey,nil];
        testMsg.parts = [NSArray arrayWithObjects:plainPart,nil];
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            [testMsg send];
        });

    }

}

- (void) timerAction : (NSTimer *) timer
{
    [_alert dismissWithClickedButtonIndex:0 animated:YES];
}

- (void) messageSent:(SKPSMTPMessage *)message
{

}

- (void) messageFailed:(SKPSMTPMessage *)message error:(NSError *)error
{
    [message release];
}

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

- (void) didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

@end
